/**
 * @file qrcode.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_QRCODE != 0

#include <stdio.h>

/*********************
 *      DEFINES
 *********************/
#define QRCODE_BG_WIDTH 180
#define QRCODE_WIDTH 126 /*21*n*/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void qrcode_prepare_destory(lv_obj_t * activity_obj);
static lv_obj_t * qrcode_create_content(lv_obj_t * par);

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
/**
 * Create a qrcode objects
 * @param parent pointer to an object, it will be the parent of the new list
 * @return pointer to the created object.
 */
lv_obj_t * qrcode_create(lv_obj_t * activity_obj)
{
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_QRCODE;
        activity_ext.create = qrcode_create;
        activity_ext.prepare_destory = qrcode_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(activity_obj == NULL) return NULL;
    }

    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return NULL;
    lv_watch_obj_set_anim_mode(obj, LV_WATCH_ANIM_HOR_RIGHT_HIDE);

    qrcode_create_content(obj);

    return obj;
}

void qrcode_create_event_cb(lv_obj_t * btn, lv_event_t event)
{
    (void)btn;

    if(LV_EVENT_CLICKED == event) {
        lv_watch_png_cache_all_free();

        lv_obj_t * obj = qrcode_create(NULL);
        LV_ASSERT_MEM(obj);
        if(obj == NULL) return;
    }
}

#if USE_LV_WATCH_LAUNCHER2 != 0
void qrcode_create_tab(lv_obj_t * par)
{
    lv_obj_t * cont = qrcode_create_content(par);
    lv_tileview_add_element(launcher_get_tileview_obj(), cont);
}
#endif

/**********************
 *   STATIC FUNCTIONS
 **********************/
static lv_obj_t * qrcode_create_content(lv_obj_t * par)
{
    lv_obj_t * content = lv_cont_create(par, NULL);
    lv_cont_set_fit(content, LV_FIT_NONE);
    lv_obj_set_size(content, QRCODE_BG_WIDTH, QRCODE_BG_WIDTH);
    lv_obj_add_style(content, LV_OBJ_PART_MAIN, &lv_watch_image);
    lv_obj_set_style_local_bg_color(content, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_cont_set_layout(content, LV_LAYOUT_OFF);
    lv_obj_set_click(content, false);
    lv_obj_align(content, par, LV_ALIGN_CENTER, 0, -10);

    /*cid.txt read from nvm*/
    char * cid = app_adaptor_get_cid_for_qrcode();
    lv_obj_t * qrcode = qrcode_gen(content, cid, 0, 0, QRCODE_WIDTH);
    lv_obj_align(qrcode, content, LV_ALIGN_CENTER, 0, 0);

    lv_obj_t * label = lv_label_create(par, NULL);
    lv_obj_add_style(label, LV_OBJ_PART_MAIN, &lv_watch_font20);
    lv_label_set_text(label, cid);
    lv_obj_set_click(label, false);
    lv_obj_align(label, content, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    return content;
}

static void qrcode_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
}

#endif /*USE_LV_WATCH_QRCODE*/
